<template>
  <div style="margin: 10px 0">
    <el-input v-model="search" style="width: 20%" placeholder="请输入关键字" clearable/>
    <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    <el-button type="primary" style="float: right;margin-right: 20px" @click="add" v-if="str.role === '管理员'">新增
    </el-button>
  </div>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="id" sortable width="100dh"  label="用户ID"/>
    <el-table-column label="用户头像" width="80dh">
      <template #default="scope">
        <!--        当前行的数据scope.row 绑定到当前行数据中的image_url字段-->
        <!--        它只在scope.row.image_url存在 没有图片就为空-->
        <img :src="scope.row.avatar" style="width: 50px; height: 50px;" v-if="scope.row.avatar"/>
      </template>
    </el-table-column>
    <el-table-column prop="username" width="120dh" label="用户名"/>
    <el-table-column prop="nickName" width="80dh" label="昵称"/>
    <el-table-column prop="gender" label="性别"/>
    <el-table-column prop="phoneNumber" width="150dh" label="电话号码"/>
    <el-table-column prop="dateOfBirth" width="180dh" label="出生日期"/>

    <el-table-column prop="address" label="地址"/>
    <el-table-column prop="createdTime" width="180dh" label="创建时间"/>
    <el-table-column prop="updatedTime" width="180dh" label="修改时间"/>

    <el-table-column fixed="right" label="操作" width="150vh" v-if="str.role === '管理员'">
      <template #default="scope">
        <el-button link type="primary" @click="handleEdit(scope.row)">重置密码</el-button>
        <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.row.id)">
          <template #reference>
            <el-button type="text" style="color: red">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>

  <!--  分页-->
  <div style="margin: 10px 0">
    <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />

    <el-dialog v-model="dialogVisible" :rules="rules" title="添加用户" width="500">
      <el-form :model="form" label-width="80px">
        <el-form-item label="头像">
          <el-upload ref="upload"
                     action="http://localhost:9090/files/upload"
                     :on-success="filesUploadSuccess"
                     :file-list="fileList"
          >
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickName" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="出生日期">
          <el-date-picker
              v-model="form.dateOfBirth"
              type="date"
              format="YYYY-MM-DD" value-format="YYYY-MM-DD"
              @change="getBirthDate"
              placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.gender" label="男">男</el-radio>
          <el-radio v-model="form.gender" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="电话号码">
          <el-input v-model="form.phoneNumber" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="地址">
          <el-input type="textarea" v-model="form.address" style="width: 80%"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.pagination {
  width: 400px; /* 或者设置一个具体的宽度，比如300px */
  margin: 30px auto; /* 水平居中 */
  text-align: center;

}
</style>

<script>
import request from "@/utils/request";

export default {
  name: 'User',
  components: {},
  data() {
    return {
      form: {},
      dialogVisible: false,
      input: '',
      search: '',
      currentPage4: 1, // 当前页
      pageSize: 10, // 每页显示条数
      total: 10,
      tableData: [],
      str: {},
      user: {
        id:"",
      },
      fileList: [],
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.str = JSON.parse(localStorage.getItem("xm-pro-employee"))
    this.user = JSON.parse(localStorage.getItem("xm-pro-user"))
    this.load()
  },
  methods: {
    filesUploadSuccess(res) {
      console.log(res)
      this.form.avatar = res.data
    },
    load() {
      request.get("/user/page", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    add() {
      this.dialogVisible = true
      this.form = {}
      this.fileList = [];
    },
    save() {
      //新增
      if(this.form.username==null){
        this.$message.success("账号不能为空")
      }else {
        request.post("/user", this.form).then(res => {
          if (res.code === '0') {
            this.$message.success("添加成功")
            this.dialogVisible = false
            this.load()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.form.password = "123456"
      console.log(this.form)
      request.put("/user/updatePassword", this.form).then(res => {
        if (res.code === '0') {
          this.$message.success("修改成功")
          this.load()
        } else {
          this.$message.error(res.msg)
        }
        this.fileList = [];
      })
    },
    handleDelete(id) {
      console.log(this.user)
      if(this.user!=null){
        if(this.user.id==id){
          this.$message.success("用户正在登录不能删除")
          return
        }
      }
        request.delete("/user/" + id).then(res => {
          if (res.code === '0') {
            this.$message.success("删除成功")
            this.load()
          } else {
            this.$message.error(res.msg)
          }
        })
    },
    handleSizeChange() {
      this.load()
    },
    handleCurrentChange() {
      this.load()
    },
    getBirthDate(date) {
      console.log(date)
      this.form.dateOfBirth = date
    }
  }
}
</script>
